package com.stylefeng.guns.rest.modular.catalogy.service;

import com.baomidou.mybatisplus.mapper.Condition;

import com.stylefeng.guns.rest.common.persistence.dao.ArtitleMapper;
import com.stylefeng.guns.rest.common.persistence.dao.CatalogyMapper;
import com.stylefeng.guns.rest.common.persistence.model.Article;
import com.stylefeng.guns.rest.common.persistence.model.Catalogy;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.ArticleListResponse;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.CatalogyHotRequest;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.CatalogyListRequest;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Created by ASUS-PC on 2018/6/27.
 */
@Service
public class CatalogyService {

    @Autowired
    CatalogyMapper catalogyMapper;
    @Autowired
    ArtitleMapper artitleMapper;

    //获取分类列表
    public List<Catalogy> queryCatalogyList(){
        return catalogyMapper.queryCatalogyList();
        /*return catalogyMapper.selectList(Condition.create().orderBy("sort",false));*/

    }

    //返回文章列表
    public List<ArticleListResponse> queryArticleList( CatalogyListRequest request){
        Map<String ,Object> data = new HashedMap();
        data.put("pageIndex" , (request.getPage()-1)*request.getSize());
        data.put("pageSize" , request.getSize());
        return artitleMapper.queryArticleList(data);
    }

    //返回某类文章列表
    public List<ArticleListResponse> queryArticleTypeList(CatalogyListRequest request){
        Map<String ,Object> data = new HashedMap();
        data.put("pageIndex" , (request.getPage()-1)*request.getSize());
        data.put("pageSize" , request.getSize());
        data.put("catalogy",request.getCatalogyId());
        return catalogyMapper.queryArticleTypeList(data);
    }

    //返回某类文章列表(支持多分类)
    public List<ArticleListResponse> queryArticleTypeList2(CatalogyListRequest request){
        Map<String ,Object> data = new HashedMap();
        data.put("pageIndex" , (request.getPage()-1)*request.getSize());
        data.put("pageSize" , request.getSize());
        data.put("catalogy",request.getCatalogyId());
        return catalogyMapper.queryArticleTypeList2(data);
    }

    //返回热门文章列表
    public List<ArticleListResponse> getCatalogyHotList(CatalogyHotRequest request){
        Map<String ,Object> data = new HashedMap();
        data.put("pageIndex" , (request.getPage()-1)*request.getSize());
        data.put("pageSize" , request.getSize());
        if(request.getType()==2){
            return catalogyMapper.querygetCatalogyHotListByMonth(data);
        }else if(request.getType()==1){
            return catalogyMapper.querygetCatalogyHotListByWeek(data);
        }
        return catalogyMapper.querygetCatalogyHotListByAll(data);
    }

    //查询分类详情
    public Catalogy queryCatalogyByName(String name){
        Map<String ,Object> data = new HashedMap();
        data.put("name",name);
        Catalogy catalogy = catalogyMapper.queryCatalogyByName(data);
        return catalogy;
    }
}
